package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.Subject;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    // 处理查询所有部门请求
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        System.out.println("当前登录用户是否有ADMIN角色："
                + SecurityUtils.getSubject().hasRole("ADMIN"));

      /*  System.out.println("当前登录用户是否有employee:delete权限："
                + SecurityUtils.getSubject().isPermitted("employee:delete"));
*/

//        /SecurityUtils.getSubject().hasRole("ADMIN")

        model.addAttribute("pageInfo", departmentService.query(qo));
        return "department/list"; // 找视图 /WEB-INF/views/department/list.jsp
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
       if (id != null) {
           departmentService.delete(id);
       }
       return new JsonResult();
    }

  /*  // 去新增或去修改
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) { // 表示去修改
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        return "department/input";
    }
*/

    // 新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){
        if(department.getId()  == null){ // 新增
            departmentService.save(department);
        }else {
            departmentService.update(department); // 修改
        }
        return new JsonResult();
    }
}
